<?php
	require_once("Zend/Loader.php");
	Zend_Loader::registerAutoLoad();
		
	# get subdomain info (if any)
	$server_parsed = split('\.', $_SERVER[SERVER_NAME]);
	$subdomain = (sizeof($server_parsed) > 2) ? $server_parsed[0] : null;
	Zend_Registry::set('subdomain', $subdomain);

	# load the application configuration
	$config_type = ($subdomain == 'modavid' 
		|| $subdomain == 'jobloggin') ? $subdomain : 'live';
	$config = new Zend_Config_Ini('../config/settings.ini', $config_type, $options);
	Zend_Registry::set('config', $config);

	# create the application logger
	$mode = (file_exists($config->path->logs->general->file)) ? 'a' : 'w';
	$logger = new Zend_Log(new Zend_Log_Writer_Stream(
		$config->path->logs->general->file), $mode);

	# change permissions of file if necessary
	if ($mode == 'w')
		chmod($config->path->logs->general->file, 0666);
	
	# store logger in registry
	Zend_Registry::set('logger', $logger);
	
	# setup application authentication
	$auth = Zend_Auth::getInstance();
	$auth->setStorage(new Zend_Auth_Storage_Session());

	# handle the user request
	$controller = Zend_Controller_Front::getInstance();
	$controller->setControllerDirectory($config->path->controllers->dir);
	$controller->registerPlugin(new CustomControllerAclManager($auth));

	# get amazon keys
	$aws_public = exec('getawskey public');
	$aws_private = exec('getawskey private');

	# create simpledb interaction object and store it in the registry
	$sdb = new SDB($aws_public, $aws_private);
	Zend_Registry::set('sdb', $sdb);

	# create s3 interaction object and store it in the registry
	$s3 = new S3($aws_public, $aws_private);
	Zend_Registry::set('s3', $s3);
	
	# setup the view render
	$vr = new Zend_Controller_Action_Helper_ViewRenderer();
	$vr->setView(new Templater());
	$vr->setViewSuffix('tpl');
	Zend_Controller_Action_HelperBroker::addHelper($vr);

	// use a config file to set up routing??
	$router = $controller->getRouter(); 

	# browse a user's media route
	$route = new Zend_Controller_Router_Route('media/:user',    
		array('controller' => 'media', 'action' => 'browse')
	);
	$router->addRoute('media', $route);

	# browse a user's comments route
	$route = new Zend_Controller_Router_Route('comments/:user',    
		array('controller' => 'comments', 'action' => 'browse')
	);
	$router->addRoute('comments', $route);

	#ini_set('display_errors', 'On'); 
	#ini_set('xdebug.dump.SERVER', 'SERVER_NAME');
	#ini_set('xdebug.var_display_max_data', '1000');
	#ini_set('xdebug.var_display_max_children', '1000');
	#ini_set('xdebug.var_display_max_depth', '1000');

	$controller->dispatch();
